package yxy.game.pm2.module.wuchao.event;

import java.util.ArrayList;
import java.util.List;

import yxy.apple.util.RandomUtility;
import yxy.apple.util.RandomUtility.Weight;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.wuchaoshop;
import yxy.cherry.data.bean.wuchaoshopdate;
import yxy.game.pm2.message.WuchaoMessage;
import yxy.game.pm2.module.shop.Goods;
import yxy.game.pm2.module.shop.WuchaoShopModule;

/** 乌巢事件 商店新增 */
public class WuchaoEventNewGoods extends WuchaoEvent {
	@Override
	public EventType getEventType() {
		return EventType.NewGoods;
	}

	@Override
	protected void _packageInfo(WuchaoMessage.WuchaoEvent.Builder builder) {

	}

	private ArrayList<Integer> goods = new ArrayList<>();// 商品id

	public ArrayList<Integer> getGoods() {
		return goods;
	}

	public void setGoods(ArrayList<Integer> goods) {
		this.goods = goods;
	}

	public static WuchaoEventNewGoods create(WuchaoShopModule shopModule) {
		WuchaoEventNewGoods event = new WuchaoEventNewGoods();
		wuchaoshopdate shopdata = DataCenter.getData(1, wuchaoshopdate.class);
		int num = RandomUtility.nextInt(shopdata.getMin(), shopdata.getMax() + 1);
		List<Commodity> list = RandomUtility.randomWeight(randomList(shopdata), null, num);

		for (Commodity commodity : list) {
			wuchaoshop data = DataCenter.getData(commodity.commodityid, wuchaoshop.class);
			Goods goods = shopModule.addGoods(data);
			event.getGoods().add(goods.getId());
		}

		return event;
	}

	private static List<Commodity> randomList(wuchaoshopdate data) {
		ArrayList<Commodity> list = new ArrayList<>();
		for (int i = 0; i < data.getPercent().length; i++) {
			list.add(new Commodity(data.getCommodityid()[i], data.getPercent()[i]));
		}
		return list;
	}

	static private class Commodity implements Weight {
		private int commodityid;
		private int percent;

		private Commodity(int commodityid, int percent) {
			this.commodityid = commodityid;
			this.percent = percent;
		}

		@Override
		public int getWeight() {
			return percent;
		}
	}
}
